//
//  StatusCell.swift
//  我的微博练习
//
//  Created by zhangguo on 15/10/14.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit

//cell枚举
enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    //根据微博函数，返回不同的cell  m枚举中想用类方法来，需要定义一个静态变量
    static func cellId(status:Status) -> String {
        
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
        
    }
}

//控件间间距
let statusCellControlMargin: CGFloat = 8.0

class StatusCell: UITableViewCell {

//微博数据模型
    var statuss: Status?{
        didSet{
            topView.status = statuss
            let statusText = statuss?.text ?? ""
            contentLabel.attributedText = EmoticonsPackage.emoticonText(statusText, font: contentLabel.font)
            
            //设置配图视图数据
            pictureImage.status = statuss

            
            pictureHightCont?.constant = pictureImage.bounds.size.height
            pictureWidthCont?.constant = pictureImage.bounds.size.width
            pictureTopCONT?.constant = (pictureImage.bounds.height == 0) ? 0 : statusCellControlMargin
        }
    }
    //记录图片宽高
    var pictureWidthCont: NSLayoutConstraint?
    var pictureHightCont: NSLayoutConstraint?
    //图片顶部约束
    var pictureTopCONT: NSLayoutConstraint?
    
    //记录行高
    func rowHeight(status:Status) -> CGFloat {
        self.statuss = status
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    //界面设置
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    func setupUI() {
        
        //添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureImage)
        contentView.addSubview(bottomView)
        
        //设置布局
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        //宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width,
            multiplier: 1.0, constant: -2 * statusCellControlMargin))
            
        //底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureImage, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
        
    }
    //懒加载控件
    private lazy var topView: StatusTopView = StatusTopView()
 
    lazy var contentLabel: UILabel = {
        let label = UILabel(Color: UIColor.darkGrayColor(), fontSize: 15)
        
        label.numberOfLines = 0
        //如果不设置此属性，行高可能有问题
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width
        return label

        }()
    //配图
     lazy var pictureImage: StatusPictureView = StatusPictureView()
    
    //底部视图
     lazy var bottomView: bottonView = bottonView()
}
